package com.cjx.print;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.Toast;

/**
 * Created by bpb on 2019/11/7.
 * 说明：
 */
public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {//开机启动广播
            Toast.makeText(context, "开机监听， 拉起服务", Toast.LENGTH_SHORT).show();
            Intent serviceIntent = new Intent(context, BackgroundService.class);
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    context.startForegroundService(serviceIntent);
                } else {
                    context.startService(serviceIntent);
                }
            } catch (Exception e) {
                // 处理后台启动限制异常
                System.out.println("系统限制从后台启动服务,无法启动服务: " + e.getMessage());
            }
        }
    }
}
